package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.QykhhDao;
import com.hnkywl.sts.entity.organ.Khh;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-5-27
 * Time: 下午6:15
 * To change this template use File | Settings | File Templates.
 */
@Service
public class QykhhService {
    private QykhhDao qykhhDao;
    private QyjgService qyjgService;
    /**
     * 查询开户行列表
     *
         * @param qyjgId   企业机构ID
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findQykhhList(Long qyjgId,String qyjgjc,String qc,String jc, int pageNo, int pageSize) {
        Page<Khh> qykhhPage = this.qykhhDao.findQykhhList(qyjgId,qyjgjc,qc,jc,pageNo, pageSize);
        String qykhhJson = BaseUtils.fetchCollectionToJson(qykhhPage.getResult(), "id", "jc", "qc", "lxdh", "zh", "zhlx.id", "zhlx.z", "qyjg.id", "qyjg.jc");
        return "{\"total\":" + qykhhPage.getTotalCount() + ",\"rows\":[" + qykhhJson + "]}";
    }

    //查找下一个排序号
    @Transactional
    public Long findNextPxh(){
        return this.qykhhDao.findMaxPxh() == null ? 0 : this.qykhhDao.findMaxPxh()+1;
    }

    /**
     * 查询开户行列表
     *
     * @param qyjgId    企业机构Id
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findQyKhhmc(Long qyjgId, int pageNo, int pageSize) {
        Page<Khh> khhPage = this.qykhhDao.findQyjgKhhList(qyjgId, pageNo, pageSize);
        List<Khh> khhList = khhPage.getResult() == null ? new ArrayList<Khh>() : khhPage.getResult();
        String khhJson = BaseUtils.fetchCollectionToJson(khhList, "id", "qc","zh");
        return "[" + khhJson + "]";
    }


    @Transactional
    public void saveQyKmdm(Khh khh){
        Khh kh = qykhhDao.get(khh.getId());
        kh.setKmdm(khh.getKmdm());
        qykhhDao.save(kh);
    }


    /**
     * 查询开户行列表
     *
     * @param khhmc    开户行名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findQyKhhmc(String khhmc,Long qyfrId, int pageNo, int pageSize) {
        Page<Khh> qykhhPage = this.qykhhDao.findQykhhList(khhmc,qyfrId, pageNo, pageSize);
        List<Khh> khhList = qykhhPage.getResult() == null ? new ArrayList<Khh>() : qykhhPage.getResult();
        String qykhhJson = BaseUtils.fetchCollectionToJson(khhList, "id", "qc");
        return "[" + qykhhJson + "]";
    }

    /**
     * 查询开户行
     *
     * @param id
     * @return
     */
    @Transactional
    public Khh findQyKhhById(Long id) {
        return this.qykhhDao.get(id);
    }

    /**
     * 保存开户行信息
     *
     * @param qykhh 开户行实体
     * @return
     */
    @Transactional
    public void saveQyKhh(Khh qykhh) {
        qykhh.setCreateUser(YgService.getCurrentYg());
        qykhh.setCreateOrgan(YgService.getCurrentYg().getZggs());
        qykhh.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        qykhh.setDateCreated(new Date());
        qykhh.setLastUpdated(qykhh.getDateCreated());
        this.qykhhDao.save(qykhh);
    }

    /**
     * 更新工程信息
     *
     * @param qykhh 开户行
     * @return
     */
    @Transactional
    public void updateQyKhh(Khh  qykhh) throws RuntimeException {
        Khh clone = this.qykhhDao.get(qykhh.getId());
        clone = (Khh) ReflectionUtils.getInstance(clone, qykhh);
        clone.setLastUpdated(new Date());
        this.qykhhDao.save(clone);
    }

    /**
     * 删除开户行信息
     *
     * @param id 开户行ID
     * @return
     */
    @Transactional
    public void deleteQyKhh(Long id) {
        Khh qykhh = qykhhDao.get(id);
        qykhh.setLastUpdated(new Date());
        qykhh.setDeleted(1);
        this.qykhhDao.save(qykhh);
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, String field) {
        return qykhhDao.checkUnique(id, value, field);
    }

    /**
     * 获取下一个同层次的排序号
     *
     * @param qyjgId 的Id
     * @return
     */
    public Long findNextPxh(Long qyjgId) {
        return qykhhDao.findMaxPxh(qyjgId) == null ? 0 : qykhhDao.findMaxPxh(qyjgId) + 1;
    }


    @Autowired
    public void setQykhhDao(QykhhDao qykhhDao) {
        this.qykhhDao = qykhhDao;
    }
}
